/**
 * 学校-课程管理-课程分类
 */
//添加父级分类
var addParentType = function() {
	$('#addParentTypeItem').show();
	$(".input_wrap").inputTips();
};

//添加子类
var addSubType = function(typeid) {
	$('#addSubTypeItem_'+typeid).show();
	$(".input_wrap").inputTips();
};

//取消添加父级分类
var canelAddParentType = function() {
	$("#parentTypeName").val('');
	$('#addParentTypeItem').hide();
};

//取消添加子类
var cancelAddSubType = function(typeid) {
	$("#subTypeName_"+typeid).val('');
	$('#addSubTypeItem_'+typeid).hide();
};

//保存添加父级分类
var saveAddParentType = function() {
	var type_name = $.trim($("#parentTypeName").val());;
	$.ajax({
		url : KK._kkurl+"/?mod=seller&act=course&do=addType"+(typeof(SID) != 'undefined' ? '&sid='+SID : ''),
		dataType : "json",
		type : "post",
		data : "pid=0&typeName="+type_name,
		success : function(rs){
			if(rs.code > 0){
				canelAddParentType();
				var str = '<li id="parentType_'+rs.data+'" class="parent_type_item">';
				str += '<div class="main_item">';
				str += '<dl class="dlList clearfix">';
				str += '<dd class="i_1">';
				str += '<span class="name">'+type_name+'</span>';
				str += '<div class="set_group clearfix">';
				str += '<a href="javascript:addSubType(\''+rs.data+'\');" class="i_add">子分类</a>';
				str += '<a href="javascript:editParentType(\''+rs.data+'\');" class="i_edit">编辑</a></div>';
				str += '</dd>';
				str += '<dd class="i_2">';
				str += '<a href="javascript:moveParentTypeBottom(\''+rs.data+'\');" class="move_e"></a>';
				str += '<a href="javascript:moveParentTypeTop(\''+rs.data+'\');" class="move_f"></a>';
				str += '<a href="javascript:moveParentTypePrev(\''+rs.data+'\');" class="move_p"></a>';
				str += '<a href="javascript:moveParentTypeNext(\''+rs.data+'\');" class="move_n"></a>';
				str += '</dd>';
				str += '<dd class="i_3"><a href="javascript:deleteParentType(\''+rs.data+'\');" class="i_del"></a></dd>';
				str += '</dl>';
				str += '<div class="input_wrap none">';
				str += '<span class="pr">';
				str += '<input type="text" class="form_text typeName" prompt="-请填写2-12个字符的分类名称-"/>';
				str += '</span>';
				str += '<a href="javascript:;" class="n_btn30g ml20 submit">保存</a>';
				str += '<a href="javascript:;" class="n_btn30gr_b ml10 cancel">取消</a>';
				str += '</div>';
				str += '</div>';
				str += '<div class="sub_item none" id="addSubTypeItem_'+rs.data+'">';
				str += '<div class="pr input_bar">';
				str += '<i class="i_tree"></i>';
				str += '<div class="input_wrap">';
				str += '<span class="pr"><input type="text" class="form_text" prompt="-请填写2-12个字符的分类名称-" id="subTypeName_'+rs.data+'"/></span>';
				str += '<a href="javascript:saveAddSubType(\''+rs.data+'\');" class="n_btn30g ml20">保存</a>';
				str += '<a href="javascript:cancelAddSubType(\''+rs.data+'\');" class="n_btn30gr_b ml10">取消</a>';
				str += '</div>';
				str += '</div>';
				str += '</div>';
				str += '</li>';
				$("#addParentTypeItem").before(str);
			}else{
				$.ckAlert({message : rs.data});
			}
		}
	});
};

//保存添加子类
var saveAddSubType = function(typeid) {
	var type_name = $.trim($("#subTypeName_"+typeid).val());;
	$.ajax({
		url : KK._kkurl+"/?mod=seller&act=course&do=addType"+(typeof(SID) != 'undefined' ? '&sid='+SID : ''),
		dataType : "json",
		type : "post",
		data : "pid="+typeid+"&typeName="+type_name,
		success : function(rs){
			if(rs.code > 0){
				cancelAddSubType(typeid);
				var str = '<div class="sub_item sub_type_item" id="subType_'+rs.data+'">';
				str += '<dl class="dlList clearfix">';
				str += '<dd class="i_1">';
				str += '<i class="i_tree"></i>';
				str += '<span class="name">'+type_name+'</span>';
				str += '<div class="set_group clearfix">';
				str += '<a href="javascript:editSubType(\''+rs.data+'\');" class="i_edit">编辑</a>';
				str += '</div>';
				str += '</dd>';
				str += '<dd class="i_2">';
				str += '<a href="javascript:moveSubTypeBottom(\''+typeid+'\', \''+rs.data+'\');" class="move_e"></a>';
				str += '<a href="javascript:moveSubTypeTop(\''+typeid+'\', \''+rs.data+'\');" class="move_f"></a>';
				str += '<a href="javascript:moveSubTypePrev(\''+typeid+'\', \''+rs.data+'\');" class="move_p"></a>';
				str += '<a href="javascript:moveSubTypeNext(\''+typeid+'\', \''+rs.data+'\');" class="move_n"></a>';
				str += '</dd>';
				str += '<dd class="i_3"><a href="javascript:deleteSubType(\''+rs.data+'\');" class="i_del"></a></dd>';
				str += '</dl>';
				str += '<div class="pr input_bar none">';
				str += '<i class="i_tree"></i>';
				str += '<div class="input_wrap">';
				str += '<span class="pr">';
				str += '<input type="text" class="form_text typeName" prompt="-请填写2-12个字符的分类名称-" id="subTypeName_'+rs.data+'"/>';
				str += '</span>';
				str += '<a href="javascript:;" class="n_btn30g ml20 submit">保存</a>';
				str += '<a href="javascript:;" class="n_btn30gr_b ml10 cancel">取消</a>';
				str += '</div>';
				str += '</div>';
				str += '</div>';
				$("#addSubTypeItem_"+typeid).before(str);
			}else{
				$.ckAlert({message : rs.data});
			}
		}
	});
};

//编辑父类
var editParentType = function(typeid) {
	var pobj = $("#parentType_"+typeid).find(".main_item");
	var listObj = pobj.find(".dlList");
	var typeName = listObj.find(".name").text();
	var inputObj = pobj.find(".input_wrap");
	listObj.hide();
	inputObj.show().find(".iptTips").hide();
	inputObj.find(".typeName").val(typeName);
	inputObj.find("a.cancel").click(function(){
		inputObj.find(".typeName").val('');
		inputObj.hide();
		listObj.show();
	});
	inputObj.find("a.submit").click(function(){
		var newTypeName = $.trim(inputObj.find(".typeName").val());
		$.ajax({
			url : KK._kkurl+"/?mod=seller&act=course&do=editType"+(typeof(SID) != 'undefined' ? '&sid='+SID : ''),
			dataType : "json",
			type : "post",
			data : "typeid="+typeid+"&typeName="+newTypeName,
			success : function(rs){
				if(rs.code > 0){
					listObj.find(".name").text(newTypeName);
					inputObj.find(".typeName").val('');
					inputObj.hide();
					listObj.show();
				}else{
					$.ckAlert({message : rs.data});
				}
			}
		});
		return false;
	});	
};

//编辑子类
var editSubType = function(typeid) {
	var pobj = $("#subType_"+typeid);
	var listObj = pobj.find(".dlList");
	var typeName = listObj.find(".name").text();
	var inputObj = pobj.find(".input_bar");
	listObj.hide();
	inputObj.show().find(".iptTips").hide();
	inputObj.find(".typeName").val(typeName);
	inputObj.find("a.cancel").click(function(){
		inputObj.find(".typeName").val('');
		inputObj.hide();
		listObj.show();
	});
	inputObj.find("a.submit").click(function(){
		var newTypeName = $.trim(inputObj.find(".typeName").val());
		$.ajax({
			url : KK._kkurl+"/?mod=seller&act=course&do=editType"+(typeof(SID) != 'undefined' ? '&sid='+SID : ''),
			dataType : "json",
			type : "post",
			data : "typeid="+typeid+"&typeName="+newTypeName,
			success : function(rs){
				if(rs.code > 0){
					listObj.find(".name").text(newTypeName);
					inputObj.find(".typeName").val('');
					inputObj.hide();
					listObj.show();
				}else{
					$.ckAlert({message : rs.data});
				}
			}
		});
		return false;
	});
};

//删除父类
var deleteParentType = function(typeid) {
	$.ckConfirm({
		message : "该操作会删除该分类下所有的子类，且不可恢复，你确定要删除分类吗？",
		ok : function(){
			$.ajax({
				url : KK._kkurl+"/?mod=seller&act=course&do=deleteType&optype=2&typeid="+typeid+"&rand="+Math.random()+(typeof(SID) != 'undefined' ? '&sid='+SID : ''),
				dataType : "json",
				type : "get",
				success : function(rs){
					if(rs.code > 0){
						$("#parentType_"+typeid).remove();
					}else{
						$.ckAlert({message : rs.data});
					}
				}
			});
		} 
	});
};

//删除子类
var deleteSubType = function(typeid) {
	$.ckConfirm({
		message : "该操作不可恢复，你确定要删除分类吗？",
		ok : function(){
			$.ajax({
				url : KK._kkurl+"/?mod=seller&act=course&do=deleteType&optype=1&typeid="+typeid+"&rand="+Math.random()+(typeof(SID) != 'undefined' ? '&sid='+SID : ''),
				dataType : "json",
				type : "get",
				success : function(rs){
					if(rs.code > 0){
						$("#subType_"+typeid).remove();
					}else{
						$.ckAlert({message : rs.data});
					}
				}
			});
		} 
	});
};

//移动父类到底部
var moveParentTypeBottom = function(typeid) {
	$.ajax({
		url : KK._kkurl+"/?mod=seller&act=course&do=sortType&op=bottom&pid=0&typeid="+typeid+"&rand="+Math.random()+(typeof(SID) != 'undefined' ? '&sid='+SID : ''),
		dataType : "json",
		type : "get",
		success : function(rs){
			if(rs.code > 0){
				var cobj = $("#parentType_"+typeid);
				var tmp = cobj.clone();
				cobj.remove();
				$("#addParentTypeItem").before(tmp);
			}else{
				$.ckAlert({message : rs.data});
			}
		}
	});
};

//移动子类到底部
var moveSubTypeBottom = function(pid, typeid) {
	$.ajax({
		url : KK._kkurl+"/?mod=seller&act=course&do=sortType&op=bottom&pid="+pid+"&typeid="+typeid+"&rand="+Math.random()+(typeof(SID) != 'undefined' ? '&sid='+SID : ''),
		dataType : "json",
		type : "get",
		success : function(rs){
			if(rs.code > 0){
				var cobj = $("#subType_"+typeid);
				var tmp = cobj.clone();
				cobj.remove();
				$("#addSubTypeItem_"+pid).before(tmp);
			}else{
				$.ckAlert({message : rs.data});
			}
		}
	});
};

//移动父类到顶部
var moveParentTypeTop = function(typeid) {
	$.ajax({
		url : KK._kkurl+"/?mod=seller&act=course&do=sortType&op=top&pid=0&typeid="+typeid+"&rand="+Math.random()+(typeof(SID) != 'undefined' ? '&sid='+SID : ''),
		dataType : "json",
		type : "get",
		success : function(rs){
			if(rs.code > 0){
				var cobj = $("#parentType_"+typeid);
				var tmp = cobj.clone();
				cobj.remove();
				$("#typelist").prepend(tmp);
			}else{
				$.ckAlert({message : rs.data});
			}
		}
	});
};

//移动子类到顶部
var moveSubTypeTop = function(pid, typeid) {
	$.ajax({
		url : KK._kkurl+"/?mod=seller&act=course&do=sortType&op=top&pid="+pid+"&typeid="+typeid+"&rand="+Math.random()+(typeof(SID) != 'undefined' ? '&sid='+SID : ''),
		dataType : "json",
		type : "get",
		success : function(rs){
			if(rs.code > 0){
				var cobj = $("#subType_"+typeid);
				var tmp = cobj.clone();
				cobj.remove();
				$("#parentType_"+pid).find(".main_item").after(tmp);
			}else{
				$.ckAlert({message : rs.data});
			}
		}
	});
};

//向上移动父类
var moveParentTypePrev = function(typeid) {
	$.ajax({
		url : KK._kkurl+"/?mod=seller&act=course&do=sortType&op=prev&pid=0&typeid="+typeid+"&rand="+Math.random()+(typeof(SID) != 'undefined' ? '&sid='+SID : ''),
		dataType : "json",
		type : "get",
		success : function(rs){
			if(rs.code > 0){
				var cobj = $("#parentType_"+typeid);
				var pobj = cobj.prev();
				if (pobj.hasClass("parent_type_item")) {
					var tmp = cobj.clone();
					cobj.remove();
					pobj.before(tmp);
				}
			}else{
				$.ckAlert({message : rs.data});
			}
		}
	});
};

//向上移动子类
var moveSubTypePrev = function(pid, typeid) {
	$.ajax({
		url : KK._kkurl+"/?mod=seller&act=course&do=sortType&op=prev&pid="+pid+"&typeid="+typeid+"&rand="+Math.random()+(typeof(SID) != 'undefined' ? '&sid='+SID : ''),
		dataType : "json",
		type : "get",
		success : function(rs){
			if(rs.code > 0){
				var cobj = $("#subType_"+typeid);
				var pobj = cobj.prev();
				if (pobj.hasClass("sub_type_item")) {
					var tmp = cobj.clone();
					cobj.remove();
					pobj.before(tmp);
				}
			}else{
				$.ckAlert({message : rs.data});
			}
		}
	});
};

//向下移动父类
var moveParentTypeNext = function(typeid) {
	$.ajax({
		url : KK._kkurl+"/?mod=seller&act=course&do=sortType&op=next&pid=0&typeid="+typeid+"&rand="+Math.random()+(typeof(SID) != 'undefined' ? '&sid='+SID : ''),
		dataType : "json",
		type : "get",
		success : function(rs){
			if(rs.code > 0){
				var cobj = $("#parentType_"+typeid);
				var pobj = cobj.next();
				if (pobj.hasClass("parent_type_item")) {
					var tmp = cobj.clone();
					cobj.remove();
					pobj.after(tmp);
				}
			}else{
				$.ckAlert({message : rs.data});
			}
		}
	});
};

//向下移动子类
var moveSubTypeNext = function(pid, typeid) {
	$.ajax({
		url : KK._kkurl+"/?mod=seller&act=course&do=sortType&op=next&pid="+pid+"&typeid="+typeid+"&rand="+Math.random()+(typeof(SID) != 'undefined' ? '&sid='+SID : ''),
		dataType : "json",
		type : "get",
		success : function(rs){
			if(rs.code > 0){
				var cobj = $("#subType_"+typeid);
				var pobj = cobj.next();
				if (pobj.hasClass("sub_type_item")) {
					var tmp = cobj.clone();
					cobj.remove();
					pobj.after(tmp);
				}
			}else{
				$.ckAlert({message : rs.data});
			}
		}
	});
};